package com.beemans.topon.demo.ui.fragments

import android.util.Log
import com.anythink.core.api.ATAdInfo
import com.anythink.core.api.AdError
import com.beemans.common.ext.jumpFragment
import com.beemans.common.ext.pt2px
import com.beemans.common.ui.fragments.CommonFragment
import com.beemans.common.utils.ToastUtils
import com.beemans.topon.ads2.AdvertManager
import com.beemans.topon.ads2.NativeAdvert
import com.beemans.topon.ads2.NativeAdvertCallback
import com.beemans.topon.ads2.NativeAdvertConfig
import com.beemans.topon.demo.R
import com.beemans.topon.demo.constant.Constant
import com.beemans.topon.demo.databinding.FragmentNativeAdBinding
import com.beemans.topon.demo.ui.view.CustomNativeRender2
import com.tiamosu.databinding.delegate.lazyDataBindingOrNull
import com.tiamosu.databinding.page.DataBindingConfig
import com.tiamosu.fly.ext.clickNoRepeat

/**
 * @author tiamosu
 * @date 2022/10/31
 */
class NativeAdFragment : CommonFragment() {
    private val dataBinding: FragmentNativeAdBinding? by lazyDataBindingOrNull { }
//    private val nativeAdConfig by lazy {
//        TpNativeAd.TpNativeAdConfig(Constant.NATIVE_ID, 375.pt2px, 270.pt2px)
//    }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.fragment_native_ad)
    }

    override fun initEvent() {
        //预加载广告
        dataBinding?.nativeAdBtnPreload?.clickNoRepeat {
            loadAdvert()

//            val config = nativeAdConfig.deepClone()?.apply { isOnlyPreload = true }
//                ?: return@setOnClickListener
//            TpNativeAd.start(this, config) {
//                onAdLoaded {
//                    val cache = TopOn.getNativeAdCaches(this@NativeAdFragment, config.placementId)
//                    Log.e("susu2", "cache:${cache.size}")
//                }
//            }
        }

        //加载广告并展示
        dataBinding?.nativeAdBtnShow?.clickNoRepeat {
            showAdvert()

//            val selfRender = dataBinding?.nativeAdViewSelfRender?.let { CustomNativeRender(it) }
//            dataBinding?.nativeAdFlAd?.loadAd(this, nativeAdConfig, selfRender)
//
//            val selfRender2 = dataBinding?.nativeAdViewSelfRender2?.let { CustomNativeRender(it) }
//            dataBinding?.nativeAdFlAd2?.loadAd(this, nativeAdConfig, selfRender2)
        }

        //跳转信息流列表
        dataBinding?.nativeAdBtnList?.clickNoRepeat {
            jumpFragment(R.id.nativeAdListFragment)
        }
    }

    /**
     * 加载广告
     */
    private fun loadAdvert() {
        val advertConfig = NativeAdvertConfig(Constant.NATIVE_ID, 375.pt2px, 270.pt2px)

        AdvertManager.loadNativeAdvert(
            this,
            advertConfig,
            null,
            null,
            object : NativeAdvertCallback() {
                override fun onAdvertLoaded(nativeAdvert: NativeAdvert) {
                    printLog("广告加载成功")
                }

                override fun onAdvertLoadFail(nativeAdvert: NativeAdvert, adError: AdError?) {
                    printLog("广告加载失败")
                }
            },
        )
    }

    /**
     * 展示广告
     */
    private fun showAdvert() {
        val advertConfig = NativeAdvertConfig(Constant.NATIVE_ID, 375.pt2px, 270.pt2px)
        advertConfig.isShowAfterLoaded = true
        advertConfig.isPreloadAdvert = true
        advertConfig.isPreloadWithoutReady = true

        val nativeSelfRender = dataBinding?.nativeAdViewSelfRender?.let { CustomNativeRender2(it) }

        AdvertManager.loadNativeAdvert(
            this,
            advertConfig,
            dataBinding?.nativeAdFlAd,
            nativeSelfRender,
            object : NativeAdvertCallback() {
                override fun onAdvertLoaded(nativeAdvert: NativeAdvert) {
                    printLog("广告加载成功")
                }

                override fun onAdvertLoadFail(nativeAdvert: NativeAdvert, adError: AdError?) {
                    printLog("广告加载失败")
                }

                override fun onAdvertShow(nativeAdvert: NativeAdvert, atAdInfo: ATAdInfo?) {
                    printLog("广告展示")
                }

                override fun onAdvertClicked(nativeAdvert: NativeAdvert, atAdInfo: ATAdInfo?) {
                    printLog("广告点击")
                }

                override fun onAdvertClose(
                    nativeAdvert: NativeAdvert,
                    atAdInfo: ATAdInfo?
                ): Boolean {
                    printLog("广告关闭")
                    return true
                }
            },
        )
    }

    private fun printLog(msg: String) {
        Log.e("susu", msg)
        ToastUtils.showShort(msg)
    }

    override fun doBusiness() {
    }
}